#计算文档字数
import os
from collections import Counter


def count_words(text:str):
    '''
    获取文本字数
    :param text: 文本
    :return: None
    '''
    # print(text)
    words_dict = dict(Counter(text))
    # 不含空格
    words_not_space = 0

    # 不含标点符号
    words_not_dot = 0

    # print(words_dict)

    for k,v in words_dict.items():
        if k != ' ' and k != '\n':
            words_not_space += v
            if k not in ',，.。、;；！!?？':
                words_not_dot += v
    print(f'不含空格：{words_not_space}个字符')
    print(f'不含标点：{words_not_dot}个字符')
    return None

def main():
    stopword = ":q"
    text_or_str = ""
    print("输入文档路径或者直接输入字符串\n【单独输入‘:q‘表示输入结束】：")
    for line in iter(input, stopword):
        text_or_str = text_or_str + line
    # print(text_or_str)
    # 读取文档
    # D:\学习\LDC\数据分析简历\梁栋昌-数据挖掘-2年工作经验.docx
    # print(os.path.exists(text_or_str))
    if os.path.exists(text_or_str):
        try:
            with open(text_or_str,'r',encoding='utf-8',errors='ignore') as f:
                text = f.read()
                # print(text)
                count_words(text)
        except UnicodeDecodeError:
            print('文件格式不对')
    else:
        count_words(text_or_str)
    
    
    con = input('是否继续？[y/n]\n')
    if con.lower() == 'y':
        main()


if __name__ == '__main__':
    main()
